import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Queue;

public class test_24_08_27 {

    public class CircularQueue<T> {
        private T[] elements;
        private int head;
        private int tail;
        private int capacity;
        private int count;

        public CircularQueue(int capacity) {
            this.capacity = capacity;
            this.elements = (T[]) new Object[capacity];
            this.head = 0;
            this.tail = -1;
            this.count = 0;
        }

        // 向队列中添加元素
        public boolean enqueue(T element) {
            if (isFull()) {
                return false;
            }
            tail = (tail + 1) % capacity;
            elements[tail] = element;
            count++;
            return true;
        }

        // 从队列中移除元素
        public T dequeue() {
            if (isEmpty()) {
                return null;
            }
            T removed = elements[head];
            elements[head] = null;
            head = (head + 1) % capacity;
            count--;
            return removed;
        }

        // 查看队列中的第一个元素
        public T peek() {
            if (isEmpty()) {
                return null;
            }
            return elements[head];
        }

        // 检查队列是否为空
        public boolean isEmpty() {
            return count == 0;
        }

        // 检查队列是否已满
        public boolean isFull() {
            return count == capacity;
        }

        // 获取队列中的元素数量
        public int size() {
            return count;
        }

        // 将队列转换为数组，方便打印
        private Object[] toArray() {
            Object[] result = new Object[count];
            int index = 0;
            for (int i = head; i != tail; i = (i + 1) % capacity) {
                result[index++] = elements[i];
            }
            if (count > 0) {
                result[index] = elements[tail];
            }
            return result;
        }
    }


    public void main(String[] args) {
        // 创建一个 CircularQueue 对象
        CircularQueue<String> queue = new CircularQueue<>(5);

        // 向队列中添加元素
        queue.enqueue("Alice");
        queue.enqueue("Bob");
        queue.enqueue("Charlie");

        System.out.println("队列中的元素: " + Arrays.toString(queue.toArray()));

        // 从队列中移除并返回第一个元素
        String removed = queue.dequeue();
        System.out.println("移除的元素: " + removed);

        // 查看队列中的第一个元素，但不移除
        String peeked = queue.peek();
        System.out.println("队列的第一个元素: " + peeked);

        // 再次查看队列中的元素
        System.out.println("队列中的元素: " + Arrays.toString(queue.toArray()));
    }

}
